package com.cqs.leetcode.array;

import com.cqs.leetcode.ds.ArraysUtils;

import java.util.*;

/**
 * lixw
 * 2021/11/5
 **/
public class TaskScheduler621 {

    public int leastInterval(char[] tasks, int n) {
        if (n == 0) {
            return tasks.length;
        }
        int[] times = new int[26];
        int maxTimes = 0;
        for (int i = 0; i < tasks.length; i++) {
            times[tasks[i] - 'A']++;
            maxTimes = Math.max(maxTimes, times[tasks[i] - 'A']);
        }
        int result = (1 + Math.abs(n)) * (maxTimes - 1);
        //最高频得字符个数
        int maxCnt = 0;
        for (int i = 0; i < 26; i++) {
            if (times[i] == maxTimes) {
                ++maxCnt;
            }
        }
        //max 取tasks.length 得情况 可以视为没有用到冷却时间， 就是扩展了部分桶得大小
        return Math.max(result + maxCnt, tasks.length);
    }

    public static void main(String[] args) {
        TaskScheduler621 task = new TaskScheduler621();
        List<Case> list = new ArrayList<>();
        list.add(new Case(new char[]{'A', 'A', 'A', 'A', 'A', 'A', 'B', 'C', 'D', 'E', 'F', 'G'}, -2));
        list.add(new Case(new char[]{'A', 'A', 'B', 'B', 'C', 'C', 'D', 'D', 'E', 'E', 'F', 'F'}, 1));
        list.add(new Case(new char[]{'A', 'A', 'B', 'B', 'C', 'C', 'D', 'D', 'E', 'E', 'F', 'G'}, 1));

        for (Case c : list) {
            int val = task.leastInterval(c.tasks, c.difference);
            System.out.println(c + " --- " + val);
        }
    }

    private static class Case {
        private char[] tasks;
        private int difference;


        public Case(char[] tasks, int difference) {
            this.tasks = tasks;
            this.difference = difference;
        }

        @Override
        public String toString() {
            return "{tasks:" + ArraysUtils.formatChars(tasks) + ", difference:" + difference + "}";
        }
    }
}
